How To Make A Python Data Entry Form |
您所在的位置:网站首页 › html form python › How To Make A Python Data Entry Form |
In the next few posts I’m planning to show you all all the CRUD (Create, Read, Update, Delete ) concepts, and this one happens to be the create aspect. I’m more than likely going to be changing this form as I need to, but this form has all the necessary qualities to create new providers and add them to the database. By default here is what you’ll see: But when you add a name in the form text boxes and click the “Submit” button, you’ll see this: Now the new values are ready to be added to the database table. That will be in my next post. Here is all the code: #!/usr/bin/python print "Content-type: text/html\n\n" print "" print "" print "Providers" print "" print "" import MySQLdb import os # Import modules for CGI handling import cgi, cgitb def init(): #read the query params params = os.environ.get('QUERY_STRING') findthis="=" #find if there is an equal sign in the query param #use int to convert the variable to a number from a text value intEquals = int(params.find(findthis)) #test for queryparam or first time you have arrived if intEquals > -1: #RECORD ALREADY EXISTS IN THE TABLE #slice notation like substr arg= params[3:] print 'show clicked record on form' print '' print '' sql = "SELECT * FROM tblProviders WHERE prv_id=" + arg print sql print '' print '' showtable() else: #CREATE THE NEW RECORD #get the field values form = cgi.FieldStorage() # Get data from fields if form.getvalue('fname'): fname = form.getvalue('fname') else: fname = "Not set" if form.getvalue('city'): city = form.getvalue('city') else: city = "Not set" ''' # Get data from fields fname = form.getvalue('fname') city = form.getvalue('city') print 'fname = ' + fname + '' print 'city = ' + city + '' ''' #insert the contents of the form into the table print "Enter New Provider:" print 'fname = ' + fname + '' print 'city = ' + city + '' print "" print "" print "" print "First Name:" print "" print "" print "" print "City:" print "" print "" print "" print "" print "" print "" print "" showtable() def showtable(): conn = MySQLdb.connect('localhost', 'username','pwd', 'erikloeb_med') cursor = conn.cursor() sql="SELECT * FROM tblProviders" cursor.execute(sql) # Get the number of rows in the result set numrows = cursor.rowcount print "" print "IDFirst NameCity" # Get and display all the rows for row in cursor: id = row[0] print '' print '' print "" + str(id) + "" #need to convert the index to a string print '' print '' + row[1] + '' print '' + row[2] + '' print '' print ''; # Close the connection conn.close() #start here: init() print "" print ""Here it is in action: http://pythoninhtmlexamples.com/files/med/providers_add_form_values.py Watch how it’s done: ![]() ![]() ![]() ![]() ![]() ![]() |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |